function [ output_args ] = game( option, times, masterMode )
clc;    % clear screen
global CANCEL_GAME;
global WIN_GAME;
global FINISH_GAME;
global CONTINUE;
global PRANDOM;
global MASTER;
global SMART;
global HUMAN;

global P1;
global P2;
global w;

if(~checkParameters(option,times))
    return;
end

CANCEL_GAME=-1;
WIN_GAME=2;
CONTINUE=1;
FINISH_GAME=3;

PRANDOM = 'random';
SMART   = 'learning';
MASTER  = 'master';
HUMAN  = 'human';

P1=1;
P2=2;

%WEIGTHS_DIMENTION=5;            % Wo + 3 variables
WEIGTHS_DIMENTION=5;            % Wo + 3 variables
MAX_MOVEMENTS=9;

LOSE=   -300;                   % Vb
WIN=    300;
DRAW=   0;

w=zeros(1,WEIGTHS_DIMENTION);   % TODO: get it from a file

w=[0.2030,0.1490,0.4180,0.1132,0.1168];

learningSet={};                 % TODO: get it from a file
Vb=DRAW;                        % vb initialization

ansplot=[0,0,0,0]; %[game,win,draw,lose]
wingame=0;
drawgame=0;
losegame=0;

command=CONTINUE;
    fix=times
    
    while(times>0 && ~(command==CANCEL_GAME))
        
        game={};        % initialize game secuence
        game.boards={}; % empty board secuence
        
        [board game command coordinates]=newBoard(option,game,w,masterMode);
                
        while(command==CONTINUE && ~(command==CANCEL_GAME))
            board(coordinates(1),coordinates(2))=1;
            game.boards{size(game.boards,2)+1}=board;
            
            if(~win(board,P1)&& size(find(board==0),1)>0)
                board=playOponent(board,option,w)
                game.boards{size(game.boards,2)+1}=board;
                
                if(~win(board,P2)&& size(find(board==0),1)>0)
                    [command coordinates]=playMaster(board,masterMode);
                    
                else
                    if(win(board,P2))
                        command=WIN_GAME;
                        display('WIN PLAYER 2 !')
                        Vb=WIN; % the computer
                        wingame=wingame+1;
                    end
                end
            else
                if(win(board,P1))
                    command=WIN_GAME;
                    display('WIN PLAYER 1 !')
                    Vb=LOSE; % the computer
                    losegame=losegame+1;
                end
            end
            if( ~win(board,P1) && ~win(board,P2) && size(find(board==0),1)==0)
                display('DRAW')
                command=FINISH_GAME;
                Vb=DRAW;
                drawgame=drawgame+1;
            end
            
        end
        
        if(~(command==CANCEL_GAME))
            game.movements=MAX_MOVEMENTS-size(find(board==0),1);
            learningSet{size(learningSet,2)+1}=game;
            
            w=learn(Vb,w,game)
            
            if(mod(times,round(fix*0.001))==0)
                games=wingame+losegame+drawgame;
                ansplot(size(ansplot,1)+1,:)=[fix-times,wingame/games,drawgame/games,losegame/games];
               
            end
        end
        
        times=times-1;
    end
    
    
    ansplot
    
    plot(ansplot(:,1),ansplot(:,2),'-dr',ansplot(:,1),ansplot(:,3),':og',ansplot(:,1),ansplot(:,4),'--sm')
    xlabel( 'games' )
    ylabel( 'results' )
    title('Game result vs random player')
    legend('win','draw','lose','Location','NorthEastOutside')
    
    print('-dpng','prueba.png')

end

function [command coordinates]=playMaster(board,masterMode)
    global HUMAN;
    
    if(strcmp(masterMode,HUMAN))
        % human master
        [command coordinates]=getCommand(board);
    else
        % random
        [command coordinates]=randomPlayer(board);    
    end

end

function [command coordinates]= randomPlayer(board)
    % random player
    MAX_POS=2;
    CONTINUE=1;
    command=CONTINUE;
    
    x=round(rand()*MAX_POS)+1;
    y=round(rand()*MAX_POS)+1;
    
    while(board(x,y)>0)
        x=round(rand()*MAX_POS)+1;
        y=round(rand()*MAX_POS)+1;
    end
    coordinates=[x,y];
end

function [board g command coordinates]=newBoard(option,game,w,masterMode)
        
        display('new game TaTeTi')
        board=newEmptyBoard();
    
        if(rand>0.5)
            board=playOponent(board,option,w)
            game.boards{size(game.boards,2)+1}=board;
        end
        g=game;
        
        [command coordinates]=playMaster(board,masterMode);
        
end




function b=playOponent(board,option,w)

global PRANDOM;
global SMART;
global MASTER;

if(strcmp(option,PRANDOM))
    % random player
    [command coordinates]=randomPlayer(board);
    board(coordinates(1),cooridnates(2))=2;
    
else if(strcmp(option,SMART))
        % something to do with weigths and current board
        % TODO
        display('smartMode');
        board=predictPosition(board,w);
    else if(strcmp(option,MASTER))
            display('Master not yet suported') 
        else
            display('Only  not yet suported') 
        end
    end
end

b=board;

end

function board=newEmptyBoard()
    board=zeros(3,3);
end

function checked=checkParameters(option, times)
    global PRANDOM;
    global SMART;
    global MASTER;

    PRANDOM = 'random';
    SMART   = 'learning';
    MASTER  = 'master';

    checked=0;
    if(~strcmp(option,PRANDOM) && ~strcmp(option,SMART) && ~strcmp(option,MASTER) || times<0)
        display('Invalid game parameters');
        display(strcat('Parameter 1: [',PRANDOM,'|',SMART,'|',MASTER,']'));
        display('Parameter 2: game sessions');
        return;
    end    
    checked=1;
end